<?php
/**
*	html的标签
*/
namespace Rainrock\Framework\kernel\Html;



class HtmlLabel{
	
	protected $style = '',$cont = '',$label='div',$attr='',$isnew=false,$title='',$css='',$close=true,$id='',$type='',$onclick='',$clickevt='',$align='',$href='',$display='',$src='',$radius='',$overflow='',$name='',$height='',$width='',$padding= array(),$margin=array();
	
	public static function get($label='')
	{
		return new HtmlLabel($label);
	}
	
	public function __construct($label='')
	{
		if($label)$this->label = $label;
	}
	
	/**
	*	显示之前处理
	*/
	protected function showBefore(){}
	
	/**
	*	添加css样式
	*/
	public function addCss($css)
	{
		if($this->css)$this->css.=' ';
		$this->css .= ''.$css.'';
		return $this;
	}
	
	/**
	*	设置内边距
	*/
	public function setPadding($val,$lx='all')
	{
		if(is_numeric($val))$val = ''.$val.'px';
		$this->padding[$lx] = $val;
		return $this;
	}
	
	/**
	*	设置外边距
	*/
	public function setMargin($val,$lx='all')
	{
		if(is_numeric($val))$val = ''.$val.'px';
		$this->margin[$lx] = $val;
		return $this;
	}
	
	/**
	*	设置宽
	*/
	public function setWidth($val)
	{
		if(is_numeric($val))$val = ''.$val.'px';
		$this->width = $val;
		return $this;
	}
	
	/**
	*	设置高
	*/
	public function setHeight($val,$overflow='')
	{
		if(is_numeric($val))$val = ''.$val.'px';
		if($overflow)$this->setOverflow($overflow);
		$this->height = $val;
		return $this;
	}
	
	/**
	*	设置背景颜色
	*/
	public function setBackgroundColor($val)
	{
		$this->addStyle('background-color', $val);
		return $this;
	}
	
	/**
	*	设置字体大小
	*/
	public function setFontsize($val)
	{
		if(is_numeric($val))$val = ''.$val.'px';
		$this->addStyle('font-size', $val);
		return $this;
	}
	
	/**
	*	设置字体颜色
	*/
	public function setFontcolor($val)
	{
		$this->addStyle('color', $val);
		return $this;
	}
	
	/**
	*	添加样式
	*/
	public function addStyle($key,$val)
	{
		$this->style .= ''.$key.':'.$val.';';
		return $this;
	}
	
	/**
	*	添加属性
	*/
	public function addAttr($str)
	{
		$this->attr .= ' '.$str.'';
		return $this;
	}
	
	/**
	*	添加属性
	*/
	public function addAttrs($key,$val)
	{
		$this->attr .= ' '.$key.'="'.$val.'"';
		return $this;
	}
	
	/**
	*	添加内容
	*/
	public function addCont($str)
	{
		if(gettype($str)=='object')$str = $str->show();
		$this->cont .= $str;
		return $this;
	}
	
	/**
	*	添加内容
	*/
	public function addBody($str)
	{
		return $this->addCont($str);
	}
	
	/**
	*	添加空格
	*/
	public function addBlank($hei)
	{
		$this->addCont(HtmlLabel::get()->setHeight($hei,'hidden'));
		return $this;
	}
	
	/**
	*	添加空格
	*/
	public function addKong()
	{
		$this->addCont('&nbsp;');
		return $this;
	}
	
	/**
	*	添加空格
	*/
	public function addBr()
	{
		$this->addCont('<br />');
		return $this;
	}
	
	/**
	*	设置id
	*/
	public function setId($id)
	{
		$this->id = $id;
		return $this;
	}
	
	/**
	*	设置Display
	*/
	public function setDisplay($str)
	{
		$this->display = $str;
		return $this;
	}
	
	/**
	*	设置标题
	*/
	public function setTitle($title)
	{
		$this->title = $title;
		return $this;
	}
	
	/**
	*	A标签链接
	*/
	public function setHref($href)
	{
		$this->href = $href;
		return $this;
	}
	
	/**
	*	新窗口打开
	*/
	public function setIsnew($isnew)
	{
		$this->isnew = $isnew;
		return $this;
	}
	
	/**
	*	设置名称
	*/
	public function setName($str)
	{
		$this->name = $str;
		return $this;
	}
	
	/**
	*	是否关闭标签
	*/
	public function setClose($bo)
	{
		$this->close = $bo;
		return $this;
	}
	
	/**
	*	录入的类型
	*/
	public function setType($val)
	{
		$this->type = $val;
		return $this;
	}
	
	/**
	*	设置点击
	*/
	public function setonClick($val)
	{
		$this->onclick = $val;
		return $this;
	}
	
	/**
	*	设置点击
	*/
	public function setClickevt($val)
	{
		$this->clickevt = $val;
		return $this;
	}
	
	/**
	*	对齐方式
	*/
	public function setAlign($val)
	{
		$this->align = $val;
		return $this;
	}
	
	/**
	*	垂直对齐方式flex的时候
	*/
	public function setValign($val='center')
	{
		$this->addStyle('align-items',$val);
		return $this;
	}
	
	/**
	*	设置图片地址
	*/
	public function setSrc($val)
	{
		$this->src  = $val;
		return $this;
	}
	
	/**
	*	设置边框圆角
	*/
	public function setRadius($val)
	{
		if(is_numeric($val))$val = ''.$val.'px';
		$this->radius  = $val;
		return $this;
	}
	
	/**
	*	设置overflow
	*/
	public function setOverflow($val)
	{
		$this->overflow  = $val;
		return $this;
	}
	
	/**
	*	设置css
	*/
	public function setCss($val)
	{
		$this->css  = $val;
		return $this;
	}
	
	/**
	*	显示
	*/
	public function show()
	{
		$this->showBefore();
		if($this->display)$this->addStyle('display', $this->display);
		if($this->radius)$this->addStyle('border-radius', $this->radius);
		if($this->overflow)$this->addStyle('overflow', $this->overflow);
		if($this->height)$this->addStyle('height', $this->height);
		if($this->width)$this->addStyle('width', $this->width);
		if($this->padding)foreach($this->padding as $lx=>$val){
			$slx = ($lx=='all') ? '': '-'.$lx.'';
			$this->addStyle('padding'.$slx.'', $val);
		}
		if($this->margin)foreach($this->margin as $lx=>$val){
			$slx = ($lx=='all') ? '': '-'.$lx.'';
			$this->addStyle('margin'.$slx.'', $val);
		}
		if($this->name)$this->addAttrs('name', $this->name);
		if($this->align)$this->addAttrs('align', $this->align);
		if($this->style)$this->addAttrs('style', $this->style);
		if($this->href)$this->addAttrs('href', $this->href);
		if($this->isnew)$this->addAttrs('target', "_blank");
		if($this->title)$this->addAttrs('title', $this->title);
		if($this->css)$this->addAttrs('class', $this->css);
		if($this->id)$this->addAttrs('id', $this->id);
		if($this->type)$this->addAttrs('type', $this->type);
		if($this->src)$this->addAttrs('src', $this->src);
		if($this->onclick)$this->addAttrs('onclick', $this->onclick);
		if($this->clickevt)$this->addAttrs('clickevt', $this->clickevt);
		$br = '';
		if(in_array($this->label, array('div','table','tr')))$br = chr(10);
		if($this->close){
			return '<'.$this->label.''.$this->attr.'>'.$this->cont.'</'.$this->label.'>'.$br.'';
		}else{
			return '<'.$this->label.''.$this->attr.' />';
		}
	}
}